package com.freemyleft.left.left_app.launcher;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.freemyleft.left.left_app.R;
import com.freemyleft.left.left_app.R2;
import com.freemyleft.left.zapp.app.AccountManager;
import com.freemyleft.left.zapp.app.IUserChecker;
import com.freemyleft.left.zapp.delegates.LeftDelegate;
import com.freemyleft.left.zapp.ui.launcher.ILauncherListener;
import com.freemyleft.left.zapp.ui.launcher.OnLauncherFinshTag;
import com.freemyleft.left.zapp.ui.launcher.ScrollLauncherTag;
import com.freemyleft.left.zapp.util.LeftPreference;
import com.freemyleft.left.zapp.util.timer.BaseTimerTask;
import com.freemyleft.left.zapp.util.timer.ITimerListener;

import java.text.MessageFormat;
import java.util.Timer;

import butterknife.BindView;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;

/**
 * Created by FreeMyleft on 2017/12/5.
 */
public class LauncherDelegate extends LeftDelegate implements ITimerListener {

    @BindView(R2.id.tv_launcher_timer)
    AppCompatTextView mTvtimer=null;

    private Timer mTimer=null;
    private int mcount=5;
    private ILauncherListener mLauncherListener=null;
    @OnClick(R2.id.tv_launcher_timer)
    void onClickTimer(){
        if(mTimer!=null){
            mTimer.cancel();
            mTimer=null;
            checkIsShowScroll();}

    }
    private  void initTimer(){
        mTimer=new Timer();
        final BaseTimerTask task=new BaseTimerTask(this);
        mTimer.schedule(task,0,1000);

    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ILauncherListener){
            mLauncherListener= (ILauncherListener) activity;
        }
    }
    @Override
    public Object setLayout() {
        return R.layout.delegate_launcher;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initTimer();

    }

    private void checkIsShowScroll(){
        if (!LeftPreference.getAppFlag(ScrollLauncherTag.HAS_FIRST_LAUNCHER_APP.name())){
            getSupportDelegate().start(new LauncherScrollDelegate(),SINGLETASK);

        }else {
            //检查用户是否登录了APp
            AccountManager.checkAccount(new IUserChecker() {
                @Override
                public void onSigIn() {
                    if (mLauncherListener!=null){
                        mLauncherListener.onLauncherFinish(OnLauncherFinshTag.SIGNED);
                    }

                }
                @Override
                public void onNotSigIn() {
                    if (mLauncherListener!=null){
                        mLauncherListener.onLauncherFinish(OnLauncherFinshTag.NOT_SIGNED);
                    }

                }
            });
        }

    }
    @Override
    public void onTimer() {
        //这么来？

        if (getProxyActivity()!=null){
            getProxyActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mTvtimer!=null){
                    mTvtimer.setText(MessageFormat.format("跳过\n{0}s",mcount));
                    mcount--;
                    if (mcount<0){
                        if(mTimer!=null){
                        mTimer.cancel();
                        mTimer=null;
                        checkIsShowScroll();
                        }
                    }

                }
            }
        });

    }
    }
    @Override
    public boolean onBackPressedSupport() {
        return true;

    }
}
